package sword_to_1;

import java.util.HashMap;
import java.util.Map;

public class LeeCode_48 {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcb"));
    }
    private static int lengthOfLongestSubstring(String s) {
        int l = 0, r = 0, ans = 0;
        Map<Character, Integer> map = new HashMap<>();
        while (r < s.length()){
            char c = s.charAt(r);
            if (!map.containsKey(c)){
                map.put(c, r);
                r++;
            }else {
                ans = Math.max(ans, r - l);
                l = map.get(c) + 1;
                r = l;
                map = new HashMap<>();
            }
        }
        return Math.max(ans, r - l);
    }
}
